package com.nlx.notes.module.controller.statistics;

import com.nlx.notes.module.bean.dto.querydb.StatisticsQueryDTO;
import com.nlx.notes.module.bean.dto.statistics.TopStatisticsDTO;
import com.nlx.notes.module.bean.dto.statistics.YearMonthStatisticsQueryDTO;
import com.nlx.notes.module.bean.dto.statistics.YearOverviewQueryDTO;
import com.nlx.notes.module.bean.vo.charts.ChartsDataVO;
import com.nlx.notes.module.bean.vo.charts.MonthChartsDataVO;
import com.nlx.notes.module.bean.vo.statistics.*;
import com.nlx.notes.module.core.util.JSONUtils;
import com.nlx.notes.module.service.statistics.CategoryMonthStatisticsManageService;
import com.nlx.notes.module.service.statistics.StatisticsManageService;
import com.nlx.notes.support.context.UserHolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.YearMonth;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/statistics")
@RequiredArgsConstructor
public class StatisticsManageController {

    final StatisticsManageService statisticsManageService;


    @GetMapping(value = "/v1/top")
    public List<TopStatisticsVO> expenseTopStatistics(TopStatisticsDTO topStatisticsDTO){
        topStatisticsDTO.setUserId(UserHolder.getUserContext().getId());
        topStatisticsDTO.setBookId(UserHolder.getUserContext().getBookId());
        log.info("查询流水top，参数：{}", JSONUtils.toString(topStatisticsDTO));
        return statisticsManageService.topStatistics(topStatisticsDTO);
    }


    /**
     * 月度流水图表
     * @return MonthChartsVO
     */
    @GetMapping(value = "/v1/month")
    public MonthChartsDataVO monthCharts(@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM") YearMonth month) {
        log.info("查询用户月度流水图表 -> {}", month);
        return statisticsManageService.monthStatisticsMetrics(month,UserHolder.getUserContext().getId(),UserHolder.getUserContext().getBookId());
    }


    /**
     * 查询年度概览
     * @param yearOverviewQueryDTO 参数
     */
    @GetMapping(value = "/v1/year/overview")
    public YearOverviewStatisticsVO yearOverviewStatistics(YearOverviewQueryDTO yearOverviewQueryDTO) {
        yearOverviewQueryDTO.setUserId(UserHolder.getUserContext().getId());
        yearOverviewQueryDTO.setBookId(UserHolder.getUserContext().getBookId());
        return statisticsManageService.yearOverviewStatistics(yearOverviewQueryDTO);
    }


    /**
     * 查询年度每月的收支统计
     * @param yearMonthStatisticsQueryDTO 年份
     * @return 每月的收支统计数据列表
     */
    @GetMapping(value = "/v1/year/months")
    public List<YearMonthStatisticsVO> yearMonthStatistics(YearMonthStatisticsQueryDTO yearMonthStatisticsQueryDTO) {
        log.info("查询用户年度每月收支统计 -> {}", JSONUtils.toString(yearMonthStatisticsQueryDTO));
        yearMonthStatisticsQueryDTO.setUserId(UserHolder.getUserContext().getId());
        yearMonthStatisticsQueryDTO.setBookId(UserHolder.getUserContext().getBookId());
        return statisticsManageService.yearMonthStatistics(yearMonthStatisticsQueryDTO);
    }


    /**
     * 查询用户历年统计数据
     * @return 历年统计数据
     */
    @GetMapping(value = "/v1/user/yearly")
    public UserYearlyStatisticsVO getUserYearlyStatistics() {
        log.info("查询用户历年统计数据");
        return statisticsManageService.getUserYearlyStatistics(UserHolder.getUserContext().getId(),UserHolder.getUserContext().getBookId());
    }
}
